#include "playvideo.h"
#include <QCoreApplication>
#include <QEventLoop>
#include <QMessageBox>
#include <QPushButton>

playvideo::playvideo(QWidget *parent) :QDialog(parent)
{
    this->resize(820, 620);
    headLabel = new QLabel(this);
    headLabel->setGeometry(QRect(325, 10, 170, 20));
    stopButton = new QPushButton(this);
    stopButton -> setText("stop");
    stopButton->setGeometry(QRect(206, 580, 100, 30));
    quitButton = new QPushButton(this);
    quitButton->setText("quit");
    quitButton->setGeometry(QRect(512, 580, 100, 30));
    showVideo = new QLabel(this);
    showVideo->setGeometry(QRect(90, 60, 640, 480));
    videoName = new QLabel(this);
    videoName->setGeometry(QRect(320, 550, 181, 20));

    /* 信号与槽函数 */
    connect(quitButton, SIGNAL(clicked()),this,SLOT(quitPlayVideo()));


}

playvideo::~playvideo()
{

}

void playvideo::quitPlayVideo(void)
{
    this->close();
}

void playvideo::display(char* fileName)
{
    FILE * fp = fopen(fileName, "rb");
    if(!fp)
    {
        return;
    }


    int inLen=0;
    unsigned char* inBuf=NULL;
    unsigned char* outBuf=NULL;
    int height=0;
    int width=0;

    inBuf = (unsigned char*)calloc ( 500000, sizeof(char));
    outBuf = (unsigned char*)calloc ( 1000000, sizeof(char));
    while(!feof(fp))
    {
        memset(outBuf,0,1000000);
        inLen = getNextNal(fp, inBuf);

        int result= dec.decode(inBuf, inLen,outBuf, &width, &height);

        if(result>=0)
        {
            QImage image= QImage(outBuf, width, height, QImage::Format_RGB16);
            if(!image.isNull())
            {
                play(image);
            }
        }
       delay(40);
    }
}

void playvideo::delay(int msec)
{
    QTime dieTime= QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
    {
        QCoreApplication::processEvents(QEventLoop::AllEvents, 1);
    }
}

void playvideo::play(QImage image)
{
    QImage img=image.scaled(showVideo->width(),showVideo->height(),Qt::KeepAspectRatio);

   showVideo->setPixmap(QPixmap::fromImage(img));
}

int playvideo::findStartCode (unsigned char *buf, int zeros_in_startcode)
{
    int info;
    int i;

    info = 1;
    for (i = 0; i < zeros_in_startcode; i++)
    {
        if(buf[i] != 0)
        {
            info = 0;
        }
    }
    if(buf[i] != 1)
    {
        info = 0;
    }
    return info;
}

int playvideo::getNextNal(FILE* fp, unsigned char* buf)
{
    int pos = 0;
    int startCodeFound = 0;
    int info2 = 0;
    int info3 = 0;

    while(!feof(fp) && (buf[pos++]=fgetc(fp))==0);

    while (!startCodeFound)
    {
        if (feof (fp))
        {
            return pos-1;
        }
        buf[pos++] = fgetc (fp);
        info3 = findStartCode(&buf[pos-4], 3);
        if(info3 != 1)
            info2 = findStartCode(&buf[pos-3], 2);
        startCodeFound = (info2 == 1 || info3 == 1);
    }
    if(info2)
    {
        fseek (fp, -3, SEEK_CUR);
        return pos - 3;
    }
    if(info3)
    {
        fseek (fp, -4, SEEK_CUR);
        return pos - 4;
    }
}
